home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / src / mosmllib / test / array.sml next >
Encoding:
Text File  |  1997-08-18  |  14.1 KB  |  353 lines  |  [TEXT/R*ch]

  1. (* test/array.sml -- some test cases for Array 
  2.    PS 1994-12-10, 1995-06-14, 1995-11-07 *)
  3.  
  4. use "auxil.sml";
  5.  
  6. local 
  7.     open Array 
  8.     infix 9 sub
  9.     val array0 = fromList []
  10. in
  11.  
  12. val a = fromList [1,11,21,31,41,51,61];
  13. val b = fromList [441,551,661];
  14. val c = fromList [1,11,21,31,41,51,61];
  15.  
  16. val test1 = check'(fn () => a<>c);
  17. val test2 = 
  18.     check'(fn () => 
  19.        array(0, 11) <> array0
  20.        andalso array(0,()) <> tabulate(0, fn _ => ())
  21.        andalso tabulate(0, fn _ => ()) <> fromList [] 
  22.        andalso fromList [] <> fromList [] 
  23.        andalso array(0, ()) <> array(0, ())
  24.        andalso tabulate(0, fn _ => ()) <> tabulate(0, fn _ => ()));
  25.  
  26. val d = tabulate(100, fn i => i mod 7 * 10 + 1);
  27.  
  28. val test3 = 
  29.     check'(fn () => d sub 27 = 61);
  30.  
  31. val test4a = (tabulate(maxLen+1, fn i => i) seq "WRONG")
  32.             handle Size => "OK" | _ => "WRONG";
  33.  
  34. val test4b = (tabulate(~1, fn i => i) seq "WRONG")
  35.             handle Size => "OK" | _ => "WRONG";
  36.  
  37. val test4c = 
  38.     check'(fn () => length (tabulate(0, fn i => i div 0)) = 0);
  39.  
  40. val test5a = 
  41.     check'(fn () => length (fromList []) = 0 andalso length a = 7);
  42. val test5b = 
  43.     check'(fn () => length array0 = 0);
  44.  
  45. val test6a = (c sub ~1 seq "WRONG") handle Subscript => "OK" | _ => "WRONG";
  46. val test6b = (c sub 7  seq "WRONG") handle Subscript => "OK" | _ => "WRONG";
  47. val test6c = check'(fn () => c sub 0 = 1);
  48.  
  49. val e = array(203, 0);
  50. val _ = (copy{src=d, si=0, dst=e, di=0,        len=NONE}; 
  51.      copy{src=b, si=0, dst=e, di=length d, len=NONE};
  52.      copy{src=d, si=0, dst=e, di=length d + length b, len=NONE});
  53.      
  54. fun a2v a = extract(a, 0, NONE);
  55. val ev = Vector.concat [a2v d, a2v b, a2v d]; (* length e = 203 *)
  56.  
  57. val test7 = check'(fn () => length e = 203);
  58.  
  59. val test8a = (update(e, ~1, 99) seq "WRONG")
  60.              handle Subscript => "OK" | _ => "WRONG";
  61. val test8b = (update(e, length e, 99) seq "WRONG")
  62.              handle Subscript => "OK" | _ => "WRONG";
  63.  
  64. val f = extract (e, 100, SOME 3);
  65.  
  66. val test9 = check'(fn () => f = a2v b);
  67.  
  68. val test9a = 
  69.     check'(fn () => ev = extract(e, 0, SOME (length e))
  70.        andalso ev = extract(e, 0, NONE));
  71. val test9b = 
  72.     check'(fn () => Vector.fromList [] = extract(e, 100, SOME 0));
  73. val test9c = (extract(e, ~1, SOME (length e))  seq "WRONG") 
  74.              handle Subscript => "OK" | _ => "WRONG"
  75. val test9d = (extract(e, length e+1, SOME 0) seq "WRONG") 
  76.              handle Subscript => "OK" | _ => "WRONG"
  77. val test9e = (extract(e, 0, SOME (length e+1)) seq "WRONG") 
  78.              handle Subscript => "OK" | _ => "WRONG"
  79. val test9f = (extract(e, 20, SOME ~1)        seq "WRONG") 
  80.              handle Subscript => "OK" | _ => "WRONG"
  81. val test9g = (extract(e, ~1, NONE)  seq "WRONG") 
  82.              handle Subscript => "OK" | _ => "WRONG"
  83. val test9h = (extract(e, length e+1, NONE) seq "WRONG") 
  84.              handle Subscript => "OK" | _ => "WRONG"
  85. val test9i = 
  86.     check'(fn () => a2v (fromList []) = extract(e, length e, SOME 0)
  87.         andalso a2v (fromList []) = extract(e, length e, NONE));
  88. val test9j =
  89.     check'(fn () => extract(e, 3, SOME(length e - 3)) = extract(e, 3, NONE));
  90.  
  91. val _ = copy{src=e, si=0, dst=e, di=0, len=NONE};
  92. val g = array(203, 9999999);
  93. val _ = copy{src=e, si=0, dst=g, di=0, len=NONE};
  94.  
  95. val test10a = check'(fn () => ev = extract(e, 0, SOME (length e)) 
  96.                andalso ev = extract(e, 0, NONE));
  97. val test10b = check'(fn () => ev = extract(g, 0, SOME (length g))
  98.               andalso ev = extract(g, 0, NONE));
  99.  
  100. val _ = copy{src=g, si=203, dst=g, di=0, len=SOME 0};
  101. val test10c = check'(fn () => ev = extract(g, 0, SOME (length g)));
  102.  
  103. val _ = copy{src=g, si=0, dst=g, di=203, len=SOME 0};
  104. val test10d = check'(fn () => ev = extract(g, 0, SOME (length g)));
  105.  
  106. val _ = copy{src=g, si=0, dst=g, di=1, len=SOME (length g-1)};
  107. val test10e = check'(fn () => a2v b = extract(g, 101, SOME 3));
  108.  
  109. val _ = copy{src=g, si=1, dst=g, di=0, len=SOME (length g-1)};
  110. val test10f = check'(fn () => a2v b = extract(g, 100, SOME 3));
  111.  
  112. val _ = copy{src=g, si=202, dst=g, di=202, len=SOME 1};
  113. val test10g = 
  114.     check'(fn () => g sub 202 = 10 * (202-1-103) mod 7 + 1);
  115. val test10h = 
  116.     check'(fn () => (copy{src=array0, si=0, dst=array0, di=0, len=SOME 0}; 
  117.              array0 <> array(0, 999999)));
  118. val test10i = 
  119.     check'(fn () => (copy{src=array0, si=0, dst=array0, di=0, len=NONE}; 
  120.              array0 <> array(0, 999999)));
  121.  
  122. val test11a = (copy{src=g, si= ~1, dst=g, di=0, len=NONE}; "WRONG") 
  123.               handle Subscript => "OK" | _ => "WRONG"
  124. val test11b = (copy{src=g, si=0, dst=g, di= ~1, len=NONE}; "WRONG") 
  125.               handle Subscript => "OK" | _ => "WRONG"
  126. val test11c = (copy{src=g, si=1, dst=g, di=0, len=NONE}; "OK") 
  127.               handle _ => "WRONG"
  128. val test11d = (copy{src=g, si=0, dst=g, di=1, len=NONE}; "WRONG") 
  129.               handle Subscript => "OK" | _ => "WRONG"
  130. val test11e = (copy{src=g, si=203, dst=g, di=0, len=NONE}; "OK") 
  131.               handle _ => "WRONG"
  132.  
  133. val test11f = (copy{src=g, si= ~1, dst=g, di=0, len=SOME (length g)}; "WRONG") 
  134.               handle Subscript => "OK" | _ => "WRONG"
  135. val test11g = (copy{src=g, si=0, dst=g, di= ~1, len=SOME (length g)}; "WRONG") 
  136.               handle Subscript => "OK" | _ => "WRONG"
  137. val test11h = (copy{src=g, si=1, dst=g, di=0, len=SOME (length g)}; "WRONG") 
  138.               handle Subscript => "OK" | _ => "WRONG"
  139. val test11i = (copy{src=g, si=0, dst=g, di=1, len=SOME (length g)}; "WRONG") 
  140.               handle Subscript => "OK" | _ => "WRONG"
  141. val test11j = (copy{src=g, si=0, dst=g, di=0, len=SOME (length g+1)}; "WRONG") 
  142.               handle Subscript => "OK" | _ => "WRONG"
  143. val test11k = (copy{src=g, si=203, dst=g, di=0, len=SOME 1}; "WRONG") 
  144.               handle Subscript => "OK" | _ => "WRONG"
  145.  
  146. local 
  147.     val v = ref 0
  148.     fun setv c = v := c;
  149.     fun addv c = v := c + !v;
  150.     fun setvi (i, c) = v := c + i;
  151.     fun addvi (i, c) = v := c + i + !v;
  152.     fun cons (x,r) = x ::  r
  153.     fun consi (i,x,r) = (i,x) ::  r
  154.     val inplist = [7,9,13];
  155.     val inp = fromList inplist
  156.     val pni = fromList (rev inplist)
  157.     fun copyinp a = 
  158.     copy{src=inp, si=0, dst=a, di=0, len=NONE}
  159. in 
  160.  
  161. val array0 = fromList [] : int array;
  162.  
  163. val test12a =
  164.     check'(fn _ =>
  165.                foldl cons [1,2] array0 = [1,2]
  166.        andalso foldl cons [1,2] inp = [13,9,7,1,2]
  167.        andalso (foldl (fn (x, _) => setv x) () inp; !v = 13));
  168.  
  169. val test12b =
  170.     check'(fn _ =>
  171.                foldr cons [1,2] array0 = [1,2]
  172.        andalso foldr cons [1,2] inp = [7,9,13,1,2]
  173.        andalso (foldr (fn (x, _) => setv x) () inp; !v = 7));
  174.  
  175. (*
  176. val test12c =
  177.     check'(fn _ =>
  178.                find (fn _ => true) array0 = NONE
  179.        andalso find (fn _ => false) inp = NONE
  180.        andalso find (fn x => x=7) inp = SOME 7
  181.        andalso find (fn x => x=9) inp = SOME 9
  182.        andalso (setv 0; find (fn x => (addv x; x=9)) inp; !v = 7+9));
  183. *)
  184. val test12d = 
  185.     check'(fn _ =>
  186.            (setv 117; app setv array0; !v = 117)
  187.        andalso (setv 0; app addv inp; !v = 7+9+13)
  188.        andalso (app setv inp; !v = 13));
  189.  
  190. val test12e = 
  191.     let val a = array(length inp, inp sub 0)
  192.     in 
  193.     check'(fn _ =>
  194.            (modify (~ : int -> int) array0; true)
  195.        andalso (copyinp a; modify ~ a; foldr (op::) [] a = map ~ inplist)
  196.        andalso (setv 117; modify (fn x => (setv x; 37)) a; !v = ~13))
  197.     end
  198.  
  199. val test13a =
  200.     check'(fn _ =>
  201.                foldli consi [] (array0, 0, NONE) = []
  202.        andalso foldri consi [] (array0, 0, NONE) = []
  203.        andalso foldli consi [] (inp, 0, NONE) = [(2,13),(1,9),(0,7)]
  204.        andalso foldri consi [] (inp, 0, NONE) = [(0,7),(1,9),(2,13)])
  205. val test13b =
  206.     check'(fn _ =>
  207.                foldli consi [] (array0, 0, SOME 0) = []
  208.        andalso foldri consi [] (array0, 0, SOME 0) = []
  209.        andalso foldli consi [] (inp, 0, SOME 0) = []
  210.        andalso foldri consi [] (inp, 0, SOME 0) = []
  211.        andalso foldli consi [] (inp, 3, SOME 0) = []
  212.        andalso foldri consi [] (inp, 3, SOME 0) = []
  213.        andalso foldli consi [] (inp, 0, SOME 3) = [(2,13),(1,9),(0,7)]
  214.        andalso foldri consi [] (inp, 0, SOME 3) = [(0,7),(1,9),(2,13)]
  215.        andalso foldli consi [] (inp, 0, SOME 2) = [(1,9),(0,7)]
  216.        andalso foldri consi [] (inp, 0, SOME 2) = [(0,7),(1,9)]
  217.        andalso foldli consi [] (inp, 1, SOME 2) = [(2,13),(1,9)]
  218.        andalso foldri consi [] (inp, 1, SOME 2) = [(1,9),(2,13)]
  219.        andalso foldli consi [] (inp, 2, SOME 1) = [(2,13)]
  220.        andalso foldri consi [] (inp, 2, SOME 1) = [(2,13)]);
  221.  
  222. val test13c = (foldli consi [] (inp, ~1, NONE) seq "WRONG")
  223.            handle Subscript => "OK" | _ => "WRONG";
  224. val test13d = (foldli consi [] (inp, 4, NONE) seq "WRONG")
  225.            handle Subscript => "OK" | _ => "WRONG";
  226. val test13e = (foldli consi [] (inp, ~1, SOME 2) seq "WRONG")
  227.            handle Subscript => "OK" | _ => "WRONG";
  228. val test13f = (foldli consi [] (inp, 4, SOME 0) seq "WRONG")
  229.            handle Subscript => "OK" | _ => "WRONG";
  230. val test13g = (foldli consi [] (inp, 0, SOME 4) seq "WRONG")
  231.            handle Subscript => "OK" | _ => "WRONG";
  232. val test13h = (foldli consi [] (inp, 2, SOME ~1) seq "WRONG")
  233.            handle Subscript => "OK" | _ => "WRONG";
  234.  
  235. val test13i = (foldri consi [] (inp, ~1, NONE) seq "WRONG")
  236.            handle Subscript => "OK" | _ => "WRONG";
  237. val test13j = (foldri consi [] (inp, 4, NONE) seq "WRONG")
  238.            handle Subscript => "OK" | _ => "WRONG";
  239. val test13k = (foldri consi [] (inp, ~1, SOME 2) seq "WRONG")
  240.            handle Subscript => "OK" | _ => "WRONG";
  241. val test13l = (foldri consi [] (inp, 4, SOME 0) seq "WRONG")
  242.            handle Subscript => "OK" | _ => "WRONG";
  243. val test13m = (foldri consi [] (inp, 0, SOME 4) seq "WRONG")
  244.            handle Subscript => "OK" | _ => "WRONG";
  245. val test13n = (foldri consi [] (inp, 2, SOME ~1) seq "WRONG")
  246.            handle Subscript => "OK" | _ => "WRONG";
  247. (*
  248. val test14a =
  249.     check'(fn _ =>
  250.        findi (fn _ => true) (array0, 0, NONE) = NONE
  251.    andalso findi (fn _ => false) (inp, 0, NONE) = NONE
  252.    andalso findi (fn (i, x) => x=9 orelse 117 div (2-i) = 0) (inp, 0, NONE)
  253.        = SOME (1,9));
  254.  
  255. val test14b =
  256.     check'(fn _ =>
  257.        findi (fn _ => true) (array0, 0, SOME 0) = NONE
  258.    andalso findi (fn _ => false) (inp, 0, NONE) = NONE
  259.    andalso findi (fn (i, x) => x=9 orelse 117 div (2-i) = 0) (inp, 0, NONE)
  260.        = SOME (1,9));
  261.  
  262. val test14c = (findi (fn _ => true) (inp, ~1, NONE) seq "WRONG")
  263.            handle Subscript => "OK" | _ => "WRONG";
  264. val test14d = (findi (fn _ => true) (inp, 4, NONE) seq "WRONG")
  265.            handle Subscript => "OK" | _ => "WRONG";
  266. val test14e = (findi (fn _ => true) (inp, ~1, SOME 2) seq "WRONG")
  267.            handle Subscript => "OK" | _ => "WRONG";
  268. val test14f = (findi (fn _ => true) (inp, 4, SOME 0) seq "WRONG")
  269.            handle Subscript => "OK" | _ => "WRONG";
  270. val test14g = (findi (fn _ => true) (inp, 0, SOME 4) seq "WRONG")
  271.            handle Subscript => "OK" | _ => "WRONG";
  272. val test14h = (findi (fn _ => true) (inp, 2, SOME ~1) seq "WRONG")
  273.            handle Subscript => "OK" | _ => "WRONG";
  274. *)
  275. val test15a = 
  276.     check'(fn _ =>
  277.            (setvi (0,117); appi setvi (array0, 0, NONE); !v = 117)
  278.        andalso (setvi (0,0); appi addvi (inp, 0, NONE); !v = 0+7+1+9+2+13)
  279.        andalso (appi setvi (inp, 0, NONE); !v = 2+13));
  280. val test15b = 
  281.     check'(fn _ =>
  282.            (setvi (0,117); appi setvi (array0, 0, SOME 0); !v = 117)
  283.        andalso (setvi (0,0); appi addvi (inp, 0, SOME 0); !v = 0)
  284.        andalso (setvi (0,0); appi addvi (inp, 3, SOME 0); !v = 0)
  285.        andalso (setvi (0,0); appi addvi (inp, 0, SOME 2); !v = 0+7+1+9)
  286.        andalso (setvi (0,0); appi addvi (inp, 1, SOME 2); !v = 1+9+2+13)
  287.        andalso (setvi (0,0); appi addvi (inp, 0, SOME 3); !v = 0+7+1+9+2+13)
  288.        andalso (appi setvi (inp, 1, SOME 2); !v = 2+13)
  289.        andalso (appi setvi (inp, 0, SOME 2); !v = 1+9)
  290.        andalso (appi setvi (inp, 0, SOME 1); !v = 0+7)
  291.        andalso (appi setvi (inp, 0, SOME 3); !v = 2+13));
  292.  
  293. val test15c = (appi setvi (inp, ~1, NONE) seq "WRONG")
  294.            handle Subscript => "OK" | _ => "WRONG";
  295. val test15d = (appi setvi (inp, 4, NONE) seq "WRONG")
  296.            handle Subscript => "OK" | _ => "WRONG";
  297. val test15e = (appi setvi (inp, ~1, SOME 2) seq "WRONG")
  298.            handle Subscript => "OK" | _ => "WRONG";
  299. val test15f = (appi setvi (inp, 4, SOME 0) seq "WRONG")
  300.            handle Subscript => "OK" | _ => "WRONG";
  301. val test15g = (appi setvi (inp, 0, SOME 4) seq "WRONG")
  302.            handle Subscript => "OK" | _ => "WRONG";
  303. val test15h = (appi setvi (inp, 2, SOME ~1) seq "WRONG")
  304.            handle Subscript => "OK" | _ => "WRONG";
  305.  
  306. val test16a = 
  307.     let val a = array(length inp, inp sub 0)
  308.     in 
  309.     check'(fn _ =>
  310.            (modifyi (op +) (array0, 0, NONE); true)
  311.        andalso (modifyi (op +) (array0, 0, SOME 0); true)
  312.        andalso (copyinp a; modifyi (op -) (a, 0, SOME 0); 
  313.             foldr (op::) [] a = [7,9,13])
  314.        andalso (copyinp a; modifyi (op -) (a, 3, SOME 0); 
  315.             foldr (op::) [] a = [7,9,13])
  316.        andalso (copyinp a; modifyi (op -) (a, 0, NONE); 
  317.             foldr (op::) [] a = [~7,~8,~11])
  318.        andalso (copyinp a; modifyi (op -) (a, 0, SOME 3); 
  319.             foldr (op::) [] a = [~7,~8,~11])
  320.        andalso (copyinp a; modifyi (op -) (a, 0, SOME 2); 
  321.             foldr (op::) [] a = [~7,~8,13])
  322.        andalso (copyinp a; modifyi (op -) (a, 1, SOME 2); 
  323.             foldr (op::) [] a = [7,~8,~11])
  324.        andalso (copyinp a; setv 117; 
  325.             modifyi (fn x => (setvi x; 37)) (a, 0, NONE); !v = 2+13)
  326.        andalso (copyinp a; setv 117; 
  327.             modifyi (fn x => (setvi x; 37)) (a, 0, SOME 3); !v = 2+13)
  328.        andalso (copyinp a; setv 117; 
  329.             modifyi (fn x => (setvi x; 37)) (a, 1, SOME 2); !v = 2+13)
  330.        andalso (copyinp a; setv 117; 
  331.             modifyi (fn x => (setvi x; 37)) (a, 0, SOME 2); !v = 1+9)
  332.        andalso (copyinp a; setv 117; 
  333.             modifyi (fn x => (setvi x; 37)) (a, 0, SOME 0); !v = 117)
  334.        andalso (copyinp a; setv 117; 
  335.             modifyi (fn x => (setvi x; 37)) (a, 3, SOME 0); !v = 117))
  336.     end
  337.  
  338. val test16b = (modifyi (op+) (inp, ~1, NONE) seq "WRONG")
  339.            handle Subscript => "OK" | _ => "WRONG";
  340. val test16c = (modifyi (op+) (inp, 4, NONE) seq "WRONG")
  341.            handle Subscript => "OK" | _ => "WRONG";
  342. val test16d = (modifyi (op+) (inp, ~1, SOME 2) seq "WRONG")
  343.            handle Subscript => "OK" | _ => "WRONG";
  344. val test16e = (modifyi (op+) (inp, 4, SOME 0) seq "WRONG")
  345.            handle Subscript => "OK" | _ => "WRONG";
  346. val test16f = (modifyi (op+) (inp, 0, SOME 4) seq "WRONG")
  347.            handle Subscript => "OK" | _ => "WRONG";
  348. val test16g = (modifyi (op+) (inp, 2, SOME ~1) seq "WRONG")
  349.            handle Subscript => "OK" | _ => "WRONG";
  350. end
  351.  
  352. end
  353.